const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar:1,

    page:1,
    limit:10,
    stuname:'',
    // 我要还书记录
    list:[],

    // 还书记录
    backlist:[],
    // 破损记录
    wornlist:[],

    student_id:'',

    img:[],
    booknum:'',
    bookname:'',
    stuname:'',
    jzname:'',
    allprice:'0',
    moneyhover:'',

    sum:false
  },
  sum:function(){
    let that = this
    let list=that.data.list
    let sum = that.data.sum
    if(sum){
      sum= false
    }else{
      sum = true
    }
    list.forEach(item => {
     item.checked=sum
   });
   that.setData({
     sum:sum,
     list:list
   })
  },
  tba:function(){
    this.setData({
      tabbar: 1
    })
    this.getlist();
  },
  tbb: function () {
    this.setData({
      tabbar: 2
    })
    this.backlist();
  },
  tbc: function () {
    this.setData({
      tabbar: 3
    })
  },
  tbd: function () {
    this.setData({
      tabbar: 4
    })
    this.wornlist();
  },
  // 确定还书
  define:function(){
    var that = this
    let student_id=[]
    that.data.list.forEach(item => {
      console.log(item)
      if(item.checked){
        
        student_id.push(item.student_id);
      }
    });
    console.log(student_id)
    wx.getStorage({
      key: 'user',
      success: function(res) {
        var setdata = {
          token: res.data.token,
          student_id:student_id.join(',')
        }
        console.log(setdata)
        wx.request({
          url: app.api + app.lendbag_back,
          data:setdata,
          method:'POST',
          success:function(e){
            console.log(e)
            if(e.data.code == 0){
              that.getlist()
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  // 选择学生
  selectstudent:function(e){
    let that = this
    let list = that.data.list
    let sum
    
    
    if(list[e.currentTarget.dataset.index].checked){
      list[e.currentTarget.dataset.index].checked = false
    }else{
      list[e.currentTarget.dataset.index].checked = true
    }
    sum = list.every((item)=>{
      return item.checked
    })
    console.log(list)
    console.log(sum);
    that.setData({
      list: list,
      sum:sum
    })
    
  },
  // 我要还书
  getlist: function () {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
          // stuname: that.data.stuname
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.reading,
          data: setdata,
          method: 'POST',
          success: function (e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.code == 0) {

              // 学生总数
              that.setData({
                count: e.data.data.count
              })
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list
                })
              } else if (e.data.data.list.length) {
                let list = that.data.list
                if(that.data.sum){                 
                  e.data.data.list.forEach(item => {
                    item.checked=true
                  });
                }
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
              } else {
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }
              // 空的
              if (!e.data.data.list.length) {
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
              }

            } else if (e.data.code == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              console.log("aaa")
            }
          },
          // fail: function(res) {},
        })
      },
    })
    
  },
  // 还书记录
  backlist: function () {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
          // stuname: '黄'
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.back_history,
          data: setdata,
          method: 'POST',
          success: function (e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.code == 0) {

              // 学生总数
              that.setData({
                count: e.data.data.count
              })
              if (that.data.page == 1) {
                that.setData({
                  backlist: e.data.data.list
                })
              } else if (e.data.data.list.length) {
                let backlist = that.data.backlist
                backlist = backlist.concat(e.data.data.list)
                that.setData({
                  backlist: backlist
                })
              } else {
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }
              // 空的
              if (!e.data.data.list.length) {
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
              }

            } else if (e.data.code == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              console.log("aaa")
            }
          },
          // fail: function(res) {},
        })
      },
    })
  },
  // 破损列表
  wornlist: function () {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.worn_history,
          data: setdata,
          method: 'POST',
          success: function (e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.code == 0) {

              // 学生总数
              that.setData({
                count: e.data.data.count
              })
              if (that.data.page == 1) {
                that.setData({
                  wornlist: e.data.data.list
                })
              } else if (e.data.data.list.length) {
                let wornlist = that.data.wornlist
                wornlist = wornlist.concat(e.data.data.list)
                that.setData({
                  wornlist: wornlist
                })
              } else {
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }
              // 空的
              if (!e.data.data.list.length) {
                wx.showToast({
                  title: '暂无更多数据',
                  icon: 'none',
                  duration: 2000
                })
              }

            } else if (e.data.code == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              console.log("aaa")
            }
          },
          // fail: function(res) {},
        })
      },
    })
  },

  // 上传照片
  upimg:function(){
    var that = this
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success(res) {
        let getimg = res.tempFiles
        let img = that.data.img
        getimg.forEach((item,index)=>{
          img.push(item.path)
        })
        console.log(img);
        that.setData({
          img: img
        })
      }
    })
  },
// 添加记录
  addlist:function(){
    var that = this
    wx.getStorage({
      key: 'user',
      success: function(res) {
        console.log(res);
        let setdata = {
          token:res.data.token,
          images: that.data.img,
          booknum: that.data.booknum,
          bookname: that.data.bookname,
          stuname: that.data.stuname,
          // jzname: that.data.jzname,
          allprice: that.data.allprice
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.worn,
          data: setdata,
          method: 'POST',
          success: function(e) {
            console.log(e);
            if(e.data.code==0){
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
              that.setData({
                img: [],
                booknum: '',
                bookname: '',
                stuname: '',
                jzname: '',
                allprice: 0,
                moneyhover:''
              })
            }else{
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          },
        })
      },
    })
  },
  booknum:function(e){
    this.setData({
      booknum:e.detail.value
    })
  },
  bookname: function (e) {
    this.setData({
      bookname: e.detail.value
    })
  },
  stuname: function (e) {
    this.setData({
      stuname: e.detail.value
    })
  },
  jzname: function (e) {
    this.setData({
      jzname: e.detail.value
    })
  },
  allprice: function (e) {
    this.setData({
      allprice: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar({})
    var that = this
    that.getlist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var number = that.data.page + 1;
    that.setData({
      page: number
    })
    console.log(that.data.page)
    that.getlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // tabbar
  goindex: function () {
    wx.reLaunch({
      url: '../index/index'
    })
  },
  gojy: function () {
    wx.reLaunch({
      url: '../jy/jy'
    })
  },
  gomine: function () {
    wx.reLaunch({
      url: '../mine/mine'
    })
  }
})